/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.bis.service;

import java.util.Calendar;
import java.util.List;

import com.jeesite.common.config.Global;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.bis.entity.BisDispatch;
import com.jeesite.modules.bis.dao.BisDispatchDao;

/**
 * 派单管理Service
 * @author Zhangning
 * @version 2019-08-24
 */
@Service
@Transactional(readOnly=true)
public class BisDispatchService extends CrudService<BisDispatchDao, BisDispatch> {
	
	/**
	 * 获取单条数据
	 * @param bisDispatch
	 * @return
	 */
	@Override
	public BisDispatch get(BisDispatch bisDispatch) {
		return super.get(bisDispatch);
	}
	
	/**
	 * 查询分页数据
	 * @param bisDispatch 查询条件
	 * @param bisDispatch.page 分页对象
	 * @return
	 */
	@Override
	public Page<BisDispatch> findPage(BisDispatch bisDispatch) {
		return super.findPage(bisDispatch);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param bisDispatch
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(BisDispatch bisDispatch) {
		super.save(bisDispatch);
	}
	
	/**
	 * 更新状态
	 * @param bisDispatch
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(BisDispatch bisDispatch) {
		super.updateStatus(bisDispatch);
	}
	
	/**
	 * 删除数据
	 * @param bisDispatch
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(BisDispatch bisDispatch) {
		super.delete(bisDispatch);
	}

	public List<BisDispatch> findListByType(BisDispatch bisDispatch) {
		return dao.findListByType(bisDispatch);
	}

	@Transactional(readOnly=false)
	public void updateOrderStatus(BisDispatch bisDispatch) {
		dao.updateOrderStatus(bisDispatch);
	}

	@Transactional(readOnly=false)
	public void updateAssignor(BisDispatch bisDispatch) {
		dao.updateAssignor(bisDispatch);
	}

	public int getCountByStatus(String orderStatus,String assignorId) {
		int overtime = Integer.parseInt(Global.getConfig("bis.dispatch.overtime.minutes", "10"));
		BisDispatch bisDispatch = new BisDispatch();
		bisDispatch.setOrderStatus(orderStatus);
		if (assignorId == null){
			bisDispatch.setDipatchType(2L);//抢单
		} else {
			bisDispatch.setDipatchType(1L);//派单
			bisDispatch.setAssignorId(assignorId);
		}
		if ("1".equals(orderStatus)){
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MINUTE, - overtime);
			bisDispatch.setOrderTime(c.getTime());
		}
		return dao.getCountByStatus(bisDispatch);
	}

	public List<BisDispatch> findListBySearch(BisDispatch bisDispatch) {
		return dao.findListBySearch(bisDispatch);
	}

	public int getAllCountByStatus(String orderStatus, String assignorId) {
		BisDispatch bisDispatch = new BisDispatch();
		bisDispatch.setOrderStatus(orderStatus);
		bisDispatch.setAssignorId(assignorId);
		return dao.getAllCountByStatus(bisDispatch);
	}
}